using JT808Protocol.Common;
using JT808Protocol.Models.Attributes;
using System;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.Location.LocationAddition
{
    /// <summary>
    /// 位置上报附加信息中的视频报警信息
    /// </summary>
    public class LaVideoAlarm_0x14 : ILocationAddition
    {
        /// <summary>
        /// 报警状态
        /// </summary>
        [JsonPropertyName("报警状态")] public uint AlarmStatus { get; set; }

        #region 报警状态分解

        /// <summary>
        /// 视频信号丢失
        /// </summary>
        [JsonPropertyName("视频信号丢失")]
        [JsonBooleanString]
        public bool IsSignalLoss { get; set; }

        /// <summary>
        /// 遮挡
        /// </summary>
        [JsonPropertyName("遮挡")]
        [JsonBooleanString]
        public bool IsSignalKeepOut { get; set; }

        /// <summary>
        /// 存储故障
        /// </summary>
        [JsonPropertyName("存储故障")]
        [JsonBooleanString]
        public bool IsStorageFailure { get; set; }

        /// <summary>
        /// 其它视频故障
        /// </summary>
        [JsonPropertyName("其它视频故障")]
        [JsonBooleanString]
        public bool IsOtherFailure { get; set; }

        /// <summary>
        /// 超员
        /// </summary>
        [JsonPropertyName("超员")]
        [JsonBooleanString]
        public bool IsOverMan { get; set; }

        /// <summary>
        /// 异常驾驶
        /// </summary>
        [JsonPropertyName("异常驾驶")]
        [JsonBooleanString]
        public bool IsAbnormalDriving { get; set; }

        /// <summary>
        /// 录像达到存储阀值
        /// </summary>
        [JsonPropertyName("录像达到存储阀值")]
        [JsonBooleanString]
        public bool IsMaxStorage { get; set; }

        #endregion 报警状态分解

        public override ILocationAddition ReadEntity(ReadOnlySpan<byte> source, int startIndex)
        {
            DataLength = (byte)(source[startIndex + 1] + 2);
            if (DataLength != 4 + 2)
            {
                //Logger.Warn($"位置附加信息的视频信息长度错误，略过解析");
                return null;
            }

            AlarmStatus = ParseHelper.Uint32(ref source, startIndex + 2);

            IsSignalLoss = ParseHelper.Bit(AlarmStatus, 0);

            IsSignalKeepOut = ParseHelper.Bit(AlarmStatus, 1);

            IsStorageFailure = ParseHelper.Bit(AlarmStatus, 2);

            IsOtherFailure = ParseHelper.Bit(AlarmStatus, 3);

            IsOverMan = ParseHelper.Bit(AlarmStatus, 4);

            IsAbnormalDriving = ParseHelper.Bit(AlarmStatus, 5);

            IsMaxStorage = ParseHelper.Bit(AlarmStatus, 6);

            return this;
        }
    }
}